package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os/exec"
	"strconv"
	"strings"
	"time"

	"golang.org/x/text/encoding/simplifiedchinese"
)

type Charset string

const (
	UTF8    = Charset("UTF-8")
	GB18030 = Charset("GB18030")
)

func breakCmdString(str string) (name string, agrs []string) {
	strs := strings.Split(str, " ")
	name = strs[0:1][0]
	agrs = strs[1:]

	return
}

// 生成区间[-m, n]的安全随机数
func RangeRandString(min, max int) string {

	return strconv.Itoa(RangeRandInt(min, max))
}

func RangeRandInt(min, max int) int {

	rand.New(rand.NewSource(time.Now().UnixNano()))
	i := rand.Intn(max-min) + min
	return i
}

func main() {
	device1 := "d29e58b7"
	device2 := "3e93cf6f"

	ch := make(chan struct{})
	count := 2 // count 表示活动的协程个数

	var devices []string

	devices = append(devices, device1)
	devices = append(devices, device2)
	for _, item := range devices {
		fmt.Println(item)
		item := item
		go func() {
			for i := 0; i < 3; i++ {
				openKuaihsou(item)
				fmt.Println(i, item)
			}
			ch <- struct{}{} // 协程结束，发出信号
		}()
	}

	for range ch {
		// 每次从ch中接收数据，表明一个活动的协程结束
		count--
		// 当所有活动的协程都结束时，关闭管道
		if count == 0 {
			close(ch)
		}
	}

}

//上滑
func RandomCmdSlideUp(device string) string {

	x1SlideUp := RangeRandString(300, 1000)
	x2SlideUp := RangeRandString(300, 1000)
	y1SlideUp := RangeRandString(900, 1200)
	y2SlideUp := RangeRandString(400, 600)
	return "adb -s " + device + " shell input swipe " + " " + x1SlideUp + " " + y1SlideUp + " " + x2SlideUp + " " + y2SlideUp
}

//上滑点击作品
func RandomCmdPointScreen(device string) string {

	xPointScreen := RangeRandString(20, 1000)
	yPointScreen := RangeRandString(300, 1600)
	return "adb -s " + device + " shell input tap " + " " + xPointScreen + " " + yPointScreen
}

func openKuaihsou(device string) {

	// cmdCheckDeviceStatus := "adb -s " + device + " get-state"

	cmdOpenApp := "adb -s " + device + " shell am start com.smile.gifmaker "     //打开快手
	cmdKillApp := "adb -s " + device + " shell am force-stop com.smile.gifmaker" //关闭快手
	cmdBack := "adb -s " + device + " shell input keyevent 4"                    //返回按键
	// cmdSlideUp := "adb -s " + device + " shell input swipe " + " " + xSlideUp + " " + y1SlideUp + " " + xSlideUp + " " + y2SlideUp //上滑
	// RandomCmdPointScreen() := "adb -s " + device + " shell input tap " + " " + xPointScreen + " " + yPointScreen //点击作品

	var selice []string

	selice = append(selice, cmdKillApp)
	selice = append(selice, cmdOpenApp)

	loopTime := RangeRandInt(3, 5)

	for i := 0; i < loopTime; i++ {

		selice = append(selice, RandomCmdSlideUp(device))
		selice = append(selice, RandomCmdPointScreen(device))
		selice = append(selice, cmdBack)
		selice = append(selice, RandomCmdSlideUp(device))
		selice = append(selice, RandomCmdSlideUp(device))
		selice = append(selice, RandomCmdPointScreen(device))
		selice = append(selice, RandomCmdSlideUp(device))
		selice = append(selice, RandomCmdSlideUp(device))
		selice = append(selice, cmdBack)
		selice = append(selice, RandomCmdSlideUp(device))
		selice = append(selice, RandomCmdPointScreen(device))
		selice = append(selice, RandomCmdSlideUp(device))
		selice = append(selice, cmdBack)

	}
	selice = append(selice, cmdKillApp)

	lookTime := RangeRandInt(3, 15)
	for _, item := range selice {
		fmt.Println(item)
		time.Sleep(time.Second * time.Duration(lookTime))
		ExecCommand(item)
	}
}
func ExecCommand(cmdString string) {
	name, agrs := breakCmdString(cmdString)
	cmd := exec.Command(name, agrs...)
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		fmt.Println(err)
		return
	}
	cmd.Start()
	in := bufio.NewScanner(stdout)
	for in.Scan() {
		cmdRe := ConvertByte2String(in.Bytes(), "GB18030")
		fmt.Println(cmdRe)
	}
	cmd.Wait()
}

func ConvertByte2String(byte []byte, charset Charset) string {
	var str string
	switch charset {
	case GB18030:
		var decodeBytes, _ = simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
		str = string(decodeBytes)
	case UTF8:
		fallthrough
	default:
		str = string(byte)
	}
	return str
}
